package me.guitar.restweb;

import org.eclipse.jetty.http.HttpCookie;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.SessionIdManager;
import org.eclipse.jetty.server.SessionManager;
import org.eclipse.jetty.server.handler.*;
import org.eclipse.jetty.server.session.AbstractSession;
import org.eclipse.jetty.server.session.AbstractSessionManager;
import org.eclipse.jetty.server.session.JDBCSessionManager;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.resource.Resource;
import org.nutz.lang.random.R;
import org.nutz.mvc.NutServlet;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.ioc.provider.ComboIocProvider;

import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.EventListener;
import java.util.Set;

@Modules(scanPackage = true)
@Ok("json")
@Fail("http:500")
@IocBy(type = ComboIocProvider.class, args = {"*anno", "me.guitar.restweb"})
public class Application {
    
    public static void main(String[] args) throws Exception {
        HandlerList handlers = new HandlerList();
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(false);
        resourceHandler.setBaseResource(Resource.newClassPathResource("static"));
        resourceHandler.setStylesheet("");
        
        ContextHandler staticContextHandler = new ContextHandler();
        staticContextHandler.setContextPath("/");
        staticContextHandler.setHandler(resourceHandler);
        handlers.addHandler(staticContextHandler);
        
        //添加servlet支持
        ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletContextHandler.setContextPath("/");
        servletContextHandler.setBaseResource(Resource.newClassPathResource("static"));
        ServletHolder servletHolder = new ServletHolder(new NutServlet());
        servletHolder.setInitParameter("modules", Application.class.getCanonicalName());
        servletContextHandler.addServlet(servletHolder, "*.ajax");
        ErrorHandler errorHandler = new NoContentOutputErrorHandler();
        servletContextHandler.setErrorHandler(errorHandler);
        handlers.addHandler(servletContextHandler);
        
        DefaultHandler defaultHandler = new DefaultHandler();
        handlers.addHandler(defaultHandler);
        int hosts = 9999;//端口号
        Server server = new Server(hosts);
        server.setHandler(handlers);
        server.start();
        server.join();
        
    }
}
